﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class GIA_Control
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public double loadGiaLeDP(ComboBox cmbDuocPham)
        {
            var ma_dp = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
            var ls = (GIA_GetByMaDuocphamTgThaydoiResult) db.GIA_GetByMaDuocphamTgThaydoi(ma_dp.MA_DUOCPHAM,  db.GIA_GetByMaDuocpham(ma_dp.MA_DUOCPHAM).Max().TG_THAYDOI).Single();
            return double.Parse(ls.GIA_LE.ToString());
        }
        public string loadGiaSiDP(ComboBox cmbDuocPham)
        {
            var ma_dp = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
            var ls = (GIA_GetByMaDuocphamTgThaydoiResult)db.GIA_GetByMaDuocphamTgThaydoi(ma_dp.MA_DUOCPHAM, db.GIA_GetByMaDuocpham(ma_dp.MA_DUOCPHAM).Max(w=>w.TG_THAYDOI)).Single();
            return ls.GIA_SI.ToString();
        }

        public void addData( string ma_dp, TextBox txtGia_Si, TextBox txtGia_Le)
        {
            if (validateData(txtGia_Si, txtGia_Le) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {

                db.GIA_Insert(ma_dp, DateTime.Now, int.Parse(txtGia_Si.Text), int.Parse(txtGia_Le.Text));
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData(TextBox txtGia_Si, TextBox txtGia_Le)
        {
            if (decimal.Parse(txtGia_Si.Text) > decimal.Parse(txtGia_Le.Text))
            {
                return false;
            }
            return true;
        }
        public void loadForm_Edit(string ma_edit, TextBox txtGia_Si, TextBox txtGia_Le)
        {

            var ls = (GIA_GetByMaDuocphamTgThaydoiResult)db.GIA_GetByMaDuocphamTgThaydoi(ma_edit, db.GIA_GetByMaDuocpham(ma_edit).Max(w => w.TG_THAYDOI)).Single();

            txtGia_Le.Text = ls.GIA_LE.ToString();
            
            txtGia_Si.Text = ls.GIA_SI.ToString();

        }
    }
}
